<template>
	<view class="container">
		<view class="content">
			<header-view></header-view>
			<view class="title">输入您的密码</view>
			<view class="tips">
				<view>请输入我们发送给您的代码</view>
				<view style="color: #0065ff;">{{form.email}}</view>
			</view>
			<view class="code_box">
				<u-code-input v-model="form.code" :maxlength="6" size="calc((590rpx)/6)" space="8" fontSize="24"
					focus></u-code-input>
				<view class="code_tips">
					<view v-if="!isTime" class="">重新发送代码({{Math.floor((time/1000))}}s)</view>
					<view v-else class="send" @tap="resend">重新发送代码</view>
				</view>
			</view>
			<view class="form">
				<view class="form_item">
					<view class="form_title">
						新密码
					</view>
					<view class="input_box flex">
						<input type="password" v-model="form.password" style="flex:1" maxlength="20" />
					</view>
				</view>
				<view class="fixed">
					<view class="done btn flex_between" @tap="reset">
						<text>完成</text>
						<uni-icons custom-prefix="iconfont" type="icon-duigou" color="#fff" size="24"></uni-icons>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import headerView from "@/components/headerView.vue"
	import {
		sendEmail,
		findPassApi
	} from '@/api/api.js'
	import code from "../../uni_modules/uview-ui/libs/config/props/code";
	export default {
		components: {
			headerView
		},
		data() {
			return {
				form: {
					email: '',
					code: '',
					password: ''
				},
				time: 60000,
				timer: null,
				isTime: false
			};
		},

		onLoad(query) {
			// 获取存储的邮箱参数
			const resetEmail = uni.getStorageSync('resetEmail');
			this.form.email = resetEmail
			this.resend()
		},

		onUnload() {
			clearInterval(this.timer)
		},
		methods: {
			resend() {
				if (this.time == 60000) {
					this.isTime = false
					this.timer = setInterval(() => {
						this.time = this.time - 30
						if (this.time <= 0) {
							clearInterval(this.timer)
							this.time = 60000
							// console.log(this.time)
							this.isTime = true
						}
					}, 30)
				}
				const event = 'findpwd'
				this.form.event = event
				sendEmail(this.form).then(res => {
					uni.showToast({
						title: res.data.msg,
						icon: "none",
					})
					this.form.code = res.data.data.code
				})
			},
			reset() {
				findPassApi(this.form).then(res => {
					if (res.data.code == 0) {
						uni.showToast({
							title: res.data.msg,
						})
					} else {
						uni.showToast({
							title: res.data.msg,
							icon: "none",
							success: () => {
								setTimeout(() => {
									uni.navigateTo({
										url: "/pages/login/login"
									})
								}, 1000)
							}
						})
					}
				})

			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		min-height: 100vh;
		height: auto;

		.content {
			padding-bottom: 40rpx;
			// position: relative;
			padding-right: 0;

			.title {
				padding-top: 60rpx;
				font-size: 30px;
				font-weight: 500;
				font-family: PM;
				line-height: 92rpx;
			}

			.tips {
				opacity: .5;
				margin-top: 20rpx;
			}

			.code_box {
				margin-top: 100rpx;
				padding-right: 40rpx;

				.code_tips {
					opacity: .5;
					margin-top: 40rpx;
					text-align: center;

					.send {
						color: #0065ff;
						text-decoration: underline;
					}
				}
			}

			.form {
				margin-top: 40rpx;

				.form_item {
					padding: 40rpx 40rpx 0 0;

					.form_title {
						font-family: PM;
						opacity: .8;
						line-height: 42rpx;
					}

					.input_box {
						width: 100%;
						margin-top: 20rpx;
						background-color: hsla(0, 0%, 92.9%, .46);
						border-radius: 20rpx;
						font-size: 30rpx;
						padding: 0 40rpx;
						height: 98rpx;
						line-height: 98rpx;
						border: 4rpx solid #3d3d3d;
						font-weight: 600;
						font-family: PM;
					}
				}

				.fixed {
					.btn {
						height: 100rpx;
						line-height: 100rpx;
						color: #fff;
						font-size: 32rpx;
						padding: 0 40rpx;
						border-radius: 20rpx;
						background-image: url("../../static/img/btn_bg.png");
						background-size: cover;
						background-repeat: no-repeat;
						overflow: hidden;
						// position: absolute;
						// right: 0;
						// left: 0;
						// bottom: 10vh;
					}

					.done {
						background-color: #0065ff;
						margin-left: 40rpx;
						margin-top: 8vh;
						border-radius: 20rpx 0 0 20rpx;
					}
				}
			}
		}
	}



	/deep/.u-code-input__item {
		border: none !important;
		background: #f7f7f7;
		border-radius: 20rpx;
		font-weight: bold;
		font-family: PM;
	}
</style>